<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layuiAdmin菜单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}"  media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">

            <div style="padding-bottom: 10px;">
                <button class="layui-btn btn-menu" data-type="add">添加菜单</button>
            </div>

            <table class="layui-table layui-form" id="menu-tree-table"></table>

        </div>
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script th:inline="javascript">
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','treeTable','form'], function(){
        var $ = layui.$
            ,form = layui.form
            ,admin = layui.admin
            ,treeTable = layui.treeTable;


        var re=treeTable.render(
            {
                elem: '#menu-tree-table',
                url: '/menu/data',
                icon_key: 'title',
                top_value:0,
                parent_key: 'parentId',
                is_checkbox: true,
                end: function(e){
                    form.render();
                },
                cols: [
                    {
                        key: 'id',
                        title: 'ID',
                        width: '15px'
                        ,align: 'center'
                    },
                    {
                        key: 'title',
                        title: '名称',
                        width: '100px'
                    },
                    {
                        key: 'icon',
                        title: '图标',
                        width: '15px'
                        ,align: 'center'
                        ,template: function(item){
                            return '<i class="layui-icon '+item.icon+'"></i>';
                        }
                    },{
                        key: 'url',
                        title: '地址',
                        width: '100px'
                    },{
                        key: 'parentId',
                        title: '父级ID',
                        width: '30px'
                        ,align: 'center'
                        ,template: function(item){
                            if(item.parentId==0){
                                return "无";
                            }
                            return item.parentId;
                        }
                    }
                    ,{
                        key : 'disabled',
                        title: '菜单状态',
                        width: '100px',
                        align: 'center'
                        ,template: function(item){
                            if(item.disabled){
                                return '<input type="checkbox" value="'+item.id+'" lay-filter="changeStatus" name="disabled" lay-skin="switch" lay-text="启用|禁用">';
                            }else{
                                return '<input type="checkbox" checked value="'+item.id+'" lay-filter="changeStatus" name="disabled"  lay-skin="switch" lay-text="启用|禁用">'
                            }
                        }
                    },{
                        title: '操作',
                        align: 'center',
                        width: '100px',
                        template: function(item){
                            var button=[
                                '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-filter="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>',
                                '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-filter="del"><i class="layui-icon layui-icon-delete"></i>删除</a>'
                            ].join("");
                            return button;
                        }
                    }]
            }
        );

        // 删除
        treeTable.on('tree(del)',function(data){
            top.layer.confirm("确定删除菜单?",function (view) {
                top.layer.close(view);
                admin.req({
                    url: "/menu/delete/"+data.item.id
                    ,type:"post"
                    ,done: function(res){
                        treeTable.render(re);
                    }
                });
            });
        });

        // 修改
        treeTable.on('tree(edit)',function(data){
            layer.open({
                type: 2
                ,title: '修改菜单信息'
                ,content: '/menu/info/'+data.item.id
                ,area: ['500px', '480px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find("#LAY-menu-submit");
                    iframeWindow.layui.form.on('submit(LAY-menu-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        admin.req({
                            url: "/menu/update"
                            ,type:"post"
                            ,data:field
                            ,done: function(res){
                                treeTable.render(re);
                            }
                        });
                        layer.close(index); //关闭弹层
                    });
                    submit.trigger('click');
                }
            });
        });

        //改变状态
        form.on('switch(changeStatus)', function(data){
            var checked=this.checked;
            admin.req({
                url: "/menu/status/change"
                ,type:"post"
                ,data: {id:this.value,status:!this.checked}
            });
        });

        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('LAY-user-back-role')
                    ,checkData = checkStatus.data; //得到选中的数据

                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }

                layer.confirm('确定删除吗？', function(index) {
                    var ids=admin.events.objToIds(checkData,"roleId");
                    admin.req({
                        url: "/menu/delete"
                        ,type:"post"
                        ,data:ids
                        ,done: function(res){
                            treeTable.render(re);
                        }
                    });
                });
            },
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加新菜单'
                    ,content: '/menu/add'
                    ,area: ['500px', '480px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find("#LAY-menu-submit");
                        iframeWindow.layui.form.on('submit(LAY-menu-submit)', function(data){
                            var field = data.field; //获取提交的字段
                            admin.req({
                                url: "/menu/add"
                                ,type:"post"
                                ,data:field
                                ,done: function(res){
                                    treeTable.render(re);
                                }
                            });
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                });
            }
        };

        $('.layui-btn.btn-menu').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

